package com.example.demo.disruptor;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;

/**
 * @author liweizhi
 * @date 2020/3/20 16:27
 */
public class Main03 {
    private static void handleFunction(LongEvent event, long sequence, boolean endOfBatch) {
        System.out.println("Event: " + event);
    }

    private static void publishFunction(LongEvent event, long sequence) {
        event.set(10000L);
    }

    public static void main(String[] args) throws Exception {
        // Specify the size of the ring buffer, must be power of 2.
        int bufferSize = 1024;

        // Construct the Disruptor
        Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, DaemonThreadFactory.INSTANCE);

        // Connect the handler,定义消费者
        disruptor.handleEventsWith(Main03::handleFunction);

        // Start the Disruptor, starts all threads running
        disruptor.start();

        // Get the ring buffer from the Disruptor to be used for publishing.
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

        // EventTranslator方式
        ringBuffer.publishEvent(Main03::publishFunction);

        System.in.read();
    }
}
